package com.example.vendingmachine.service.impl;

import com.example.vendingmachine.dto.AreaDTO;
import com.example.vendingmachine.dto.AreaQuery;
import com.example.vendingmachine.entity.Area;
import com.example.vendingmachine.entity.User;
import com.example.vendingmachine.mapper.AreaMapper;
import com.example.vendingmachine.service.AreaService;
import com.example.vendingmachine.service.UserService;
import com.example.vendingmachine.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 区域服务实现类
 */
@Service
@RequiredArgsConstructor
public class AreaServiceImpl implements AreaService {

    private final AreaMapper areaMapper;
    private final UserService userService;

    @Override
    public PageInfo<Area> listAreas(AreaQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return new PageInfo<>(areaMapper.selectList(query));
    }

    @Override
    public Area getAreaById(Long id) {
        Area area = areaMapper.selectById(id);
        if (area == null) {
            throw new BusinessException("区域不存在");
        }
        return area;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createArea(AreaDTO areaDTO) {
        // 检查管理员是否存在且是运营人员
        checkAdmin(areaDTO.getAdminId());
        // 检查管理员是否已被分配
        if (!checkAdminAvailable(areaDTO.getAdminId())) {
            throw new BusinessException("该管理员已被分配");
        }

        Area area = new Area();
        BeanUtils.copyProperties(areaDTO, area);
        // 设置状态为启用
        area.setStatus("ENABLED");

        areaMapper.insert(area);
        return area.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateArea(AreaDTO areaDTO) {
        // 检查区域是否存在
        Area existArea = getAreaById(areaDTO.getId());
        
        // 如果更换了管理员
        if (!existArea.getAdminId().equals(areaDTO.getAdminId())) {
            // 检查新管理员
            checkAdmin(areaDTO.getAdminId());
            // 检查新管理员是否已被分配
            if (!checkAdminAvailable(areaDTO.getAdminId())) {
                throw new BusinessException("该管理员已被分配");
            }
        }

        Area area = new Area();
        BeanUtils.copyProperties(areaDTO, area);
        areaMapper.update(area);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteArea(Long id) {
        // 检查区域是否存在
        getAreaById(id);
        // TODO: 检查区域下是否有点位
        areaMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAreaStatus(Long id, String status) {
        // 检查区域是否存在
        getAreaById(id);
        areaMapper.updateStatus(id, status);
    }

    @Override
    public boolean checkAdminAvailable(Long adminId) {
        return areaMapper.countByAdminId(adminId) == 0;
    }

    /**
     * 检查管理员是否存在且是运营人员
     */
    private void checkAdmin(Long adminId) {
        User admin = userService.getUserById(adminId);
        if (!"OPERATOR".equals(admin.getRole())) {
            throw new BusinessException("管理员必须是运营人员");
        }
    }
} 